local luastar = @@lib(lua-star)
local draft = @@lib(draft)()
local Vector = @@lib(brinevector)

local Path = @@CLASS("Path")

local PathImg = love.graphics.newImage("assets/images/ui.png")
local ImgQuad = $LG.newQuad(0, 0, 8, 8, PathImg:getDimensions())

Path.draw = function (self)
   if not self.pdata then return end
   @@SET_HEX_CL(#FFFFFF)
   lume.reduce(self.pdata, function (u, v)
		  local lux, luy, lvx, lvy = u.x*8+4, u.y*8+4, v.x*8+4, v.y*8+4
		  local rot = Vector(v.x-u.x, v.y-u.y):getAngle()
		  $LG.draw(PathImg, ImgQuad, lux, luy, rot)
		  return v
   end)
   @@SET_HEX_CL(#FFFFFF)
end

Path.tile_is_passable = function (self, x, y)
   return true
end

Path.get_path_to = function (self, x, y, x2, y2)
   local is_passable = function (x, y) return self:tile_is_passable (x, y) end
   local w, h = Game.current_map:get_dimensions()
   w = w/8
   h = h/8
   local start, goal = {x = x/8, y = y/8}, {x = x2/8, y = y2/8}
   local path = luastar:find(w, h, start, goal, is_passable, true)
   self.pdata = lume.slice(path, 2)
   return self.pdata
end

Path.get_next_point = function (self)
   if self.pdata then return self.pdata[1] end
end

Path.advance_path = function (self)
   if self.pdata then
      local len = #self.pdata
      if len >= 2 then
	 self.pdata = @@REST(self.pdata)
	 return self.pdata
      else
	 local point = self.pdata[1]
	 self.pdata = nil
	 return point
      end
   end
end

return Path
